package com.controller;

import com.entity.Role;
import com.entity.Storage;
import com.entity.Supplier;
import com.service.SupplierService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/sys/supplier")
public class SupplierController {
    @Resource
    private SupplierService supplierService;

    @RequestMapping("/list")
    public ModelAndView finSupplier(){
        ModelAndView modelAndView = new ModelAndView();
        List<Supplier> list = supplierService.finSupplier();
        modelAndView.addObject("supplierList",list);
        modelAndView.setViewName("supplier/list");
        return modelAndView;
    }


    @RequestMapping("/view")
    public ModelAndView supplierView(@RequestParam(required = true) int id,String name){
        ModelAndView modelAndView = new ModelAndView();
        Supplier supplier = supplierService.supplierId(id);
        modelAndView.addObject("supplierView",supplier);
        if (name.equals("查看")) {
            modelAndView.setViewName("supplier/view");
        }else {
            modelAndView.setViewName("supplier/update");
        }
        return modelAndView;
    }

    @RequestMapping("/update")
    public String updateSupplier(Supplier supplier){
        Supplier supplier1  = supplier;
        int update = supplierService.updSupplierDao(supplier1);
        if (update>0){
            return "frame";
        }else {
            return "supplier/update";
        }
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "supplier/add";
    }

    @RequestMapping("/add")
    public String add(Supplier supplier){
        int add = supplierService.insertSupplier(supplier);
        if (add>0){
            return "frame";
        }else {
            return "supplier/update";
        }
    }

    @RequestMapping("/wher")
    public ModelAndView wher(String querySupCode,String querySupName){
        ModelAndView modelAndView = new ModelAndView();
        List<Supplier> list = supplierService.listSupplier(querySupCode, querySupName);
        modelAndView.addObject("supplierList",list);
        modelAndView.setViewName("supplier/list");
        return modelAndView;
    }
}
